

#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "gpio.h"
#include "gd32f10x.h"
#include "stdio.h"
#include "message_buffer.h"
#include "stream_buffer.h"
#include "semphr.h"
TaskHandle_t start_task_Handler;
void start_task(void *pvParameters);

TaskHandle_t tx_Handler;
void que_tx(void *pvParameters);

TaskHandle_t tx1_Handler;
void que_tx1(void *pvParameters);

TaskHandle_t rx_Handler;
void que_rx(void *pvParameters);

TaskHandle_t rx1_Handler;
void que_rx1(void *pvParameters);


static SemaphoreHandle_t mutex1 ;

static StreamBufferHandle_t stream_1;
	

void que_start_os(void)
{

	xTaskCreate(start_task,"start_task",128,NULL,1,&start_task_Handler);
	vTaskStartScheduler();
}



void start_task(void *pvParameters)
{
	BaseType_t err;

	taskENTER_CRITICAL();
	mutex1 = xSemaphoreCreateMutex();
	stream_1 = xStreamBufferCreate(100,12);

//	err = xTaskCreate(que_tx,"tx_task",128,NULL,1,&tx_Handler);
//	if(err == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
//		printf("que_tx requires memory\r\n");
	
	err = xTaskCreate(que_tx1,"tx_task",128,NULL,2,&tx1_Handler);
	if(err == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
		printf("que_tx requires memory\r\n");

	err =xTaskCreate(que_rx,"rx_task",128,NULL,5,&rx_Handler);
	if(err == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
		printf("que_rx requires memory\r\n");
	
//	err =xTaskCreate(que_rx1,"rx1_task",128,NULL,6,&rx1_Handler);
//	if(err == errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY)
//		printf("que_rx requires memory\r\n");	
	
	size_t size = xPortGetFreeHeapSize();
	printf("free heap size is :%d \r\n",size);
	vTaskDelete(start_task_Handler);
	taskEXIT_CRITICAL();
}

void que_tx(void *pvParameters)
{
	uint8_t tx[ 12 ]={1,2,3,4,5,6,7,8,9,10,11,12};
	uint8_t tx1[ 8 ]={2,3,4,5,6,7,8,9};
	uint32_t err;
	while(1)
	{
		err = xStreamBufferSend(stream_1,tx,sizeof(tx),portMAX_DELAY);
		if(err >0)
			printf("que_tx %d:queue sende\r\n",err);
		vTaskDelay(400);
	}

}
void que_tx1(void *pvParameters)
{
	uint8_t tx[ 8 ]={21,22,23,24,25,26,27,28};
	uint32_t err;
	while(1)
	{
		err = xStreamBufferSend(stream_1,tx,sizeof(tx),portMAX_DELAY);
		if(err >0)
			printf("que_tx1 %d:queue sende\r\n",err);
		

		vTaskDelay(400);
	}

}

void que_rx(void *pvParameters)
{
	uint8_t rx[ 11 ]={0};
	uint32_t err;
	while(1)
	{
		err = xStreamBufferReceive(stream_1,rx,sizeof(rx),portMAX_DELAY);
		xSemaphoreTake(mutex1,portMAX_DELAY);
		if(err >0)
		{
		printf("que_rx:rx_data %d <-- ,",err);
			for (int i = 0;i<sizeof(rx);i++)
			{
				printf("%d ",rx[i]);
				rx[i] = 0;
			}

			printf("\r\n");
		
		}
		xSemaphoreGive(mutex1);
//		vTaskDelay(100);
	}

}


void que_rx1(void *pvParameters)
{
	uint8_t rx[ 8 ]={0};
	uint32_t err;
	while(1)
	{
		err = xStreamBufferReceive(stream_1,rx,sizeof(rx),0);
		xSemaphoreTake(mutex1,portMAX_DELAY);
		if(err >0)
		{
		printf("que_rx1:rx_data %d <-- ,",err);
			for (int i = 0;i<8;i++)
				printf("%d ",rx[i]);
			printf("\r\n");
		
		}
		xSemaphoreGive(mutex1);
		vTaskDelay(800);
	}

}
static	uint8_t test[ 10 ]={22,23,24,25,26,27,28,29,30,31};
void TIMER0_UP_IRQHandler(void)
{
	BaseType_t flag = pdFALSE;
   if( timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_UP) == SET)
   {
	   if(stream_1!=NULL)
		   xStreamBufferSendFromISR(stream_1,test,sizeof(test),&flag);

   
   }
	timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);
   portYIELD_FROM_ISR(flag);
}









